{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def begin_by(interval=None):\n",
    "    end_time = datetime.datetime.now()\n",
    "    if interval is None:\n",
    "        # 20点及以后，选当天的20点作为开始时间\n",
    "        if end_time.hour >= 20:\n",
    "            start_time = datetime.datetime(\n",
    "                end_time.year, end_time.month, end_time.day, 20, 0, 0)\n",
    "        # 8点前，选前一天的20点作为开始时间\n",
    "        elif end_time.hour < 8:\n",
    "            start_time = datetime.datetime(\n",
    "                end_time.year, end_time.month, end_time.day - 1, 20, 0, 0)\n",
    "        else:\n",
    "            start_time = datetime.datetime(\n",
    "                end_time.year, end_time.month, end_time.day, 8, 0, 0)\n",
    "    else:\n",
    "        start_time = end_time - datetime.timedelta(minutes=interval)\n",
    "    start = start_time.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "    end = end_time.strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "    return start, end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('2020-05-29 20:00:00', '2020-05-30 07:52:37')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start, end = begin_by()\n",
    "start, end "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "line = 'w7'\n",
    "sql_pause = \"select * from analyse_pause where StartTime between '{0}' AND '{1}' AND StartLine Like '{2}' AND isActive = 1;\".format(\n",
    "        start, end, line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sqlalchemy import create_engine\n",
    "from sqlalchemy.ext.declarative import declarative_base\n",
    "from sqlalchemy.orm import sessionmaker\n",
    "SQLALCHEMY_DATABASE_URL = \"mysql+pymysql://root:yongs81@106.52.146.218:3306/demo\"\n",
    "engine = create_engine(SQLALCHEMY_DATABASE_URL, echo=True, encoding='utf-8', convert_unicode=True)\n",
    "SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)\n",
    "Base = declarative_base()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true,
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,579 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'sql_mode'\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,583 INFO sqlalchemy.engine.base.Engine {}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,592 INFO sqlalchemy.engine.base.Engine SHOW VARIABLES LIKE 'lower_case_table_names'\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,594 INFO sqlalchemy.engine.base.Engine {}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,611 INFO sqlalchemy.engine.base.Engine SELECT DATABASE()\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,612 INFO sqlalchemy.engine.base.Engine {}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,635 INFO sqlalchemy.engine.base.Engine show collation where `Charset` = 'utf8mb4' and `Collation` = 'utf8mb4_bin'\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,636 INFO sqlalchemy.engine.base.Engine {}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,645 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS CHAR(60)) AS anon_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,646 INFO sqlalchemy.engine.base.Engine {}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,655 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS CHAR(60)) AS anon_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,656 INFO sqlalchemy.engine.base.Engine {}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,665 INFO sqlalchemy.engine.base.Engine SELECT CAST('test collated returns' AS CHAR CHARACTER SET utf8mb4) COLLATE utf8mb4_bin AS anon_1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,666 INFO sqlalchemy.engine.base.Engine {}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,690 INFO sqlalchemy.engine.base.Engine select * from analyse_pause where StartTime between '2020-05-29 20:00:00' AND '2020-05-30 07:52:37' AND StartLine Like 'w7' AND isActive = 1;\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-05-30 07:52:47,691 INFO sqlalchemy.engine.base.Engine {}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>StartLine</th>\n",
       "      <th>StartVarName</th>\n",
       "      <th>StartAlarm</th>\n",
       "      <th>StartID</th>\n",
       "      <th>StartTime</th>\n",
       "      <th>EndLine</th>\n",
       "      <th>EndVarName</th>\n",
       "      <th>EndAlarm</th>\n",
       "      <th>EndID</th>\n",
       "      <th>EndTime</th>\n",
       "      <th>Interval</th>\n",
       "      <th>isActive</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>429186</td>\n",
       "      <td>w7</td>\n",
       "      <td>吹水段到静音房急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618198</td>\n",
       "      <td>2020-05-29 20:01:25</td>\n",
       "      <td>w7</td>\n",
       "      <td>吹水段到静音房急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618203</td>\n",
       "      <td>2020-05-29 20:01:51</td>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>429187</td>\n",
       "      <td>w7</td>\n",
       "      <td>真空线出口急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618093</td>\n",
       "      <td>2020-05-29 20:00:00</td>\n",
       "      <td>w7</td>\n",
       "      <td>真空线出口急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618199</td>\n",
       "      <td>2020-05-29 20:01:38</td>\n",
       "      <td>98</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>429189</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618097</td>\n",
       "      <td>2020-05-29 20:00:07</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618194</td>\n",
       "      <td>2020-05-29 20:01:00</td>\n",
       "      <td>53</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>429193</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618202</td>\n",
       "      <td>2020-05-29 20:01:48</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618299</td>\n",
       "      <td>2020-05-29 20:03:42</td>\n",
       "      <td>114</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>429196</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618403</td>\n",
       "      <td>2020-05-29 20:05:15</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618421</td>\n",
       "      <td>2020-05-29 20:06:00</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>StartLine</th>\n",
       "      <th>StartVarName</th>\n",
       "      <th>StartAlarm</th>\n",
       "      <th>StartID</th>\n",
       "      <th>StartTime</th>\n",
       "      <th>EndLine</th>\n",
       "      <th>EndVarName</th>\n",
       "      <th>EndAlarm</th>\n",
       "      <th>EndID</th>\n",
       "      <th>EndTime</th>\n",
       "      <th>Interval</th>\n",
       "      <th>isActive</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>429186</td>\n",
       "      <td>w7</td>\n",
       "      <td>吹水段到静音房急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618198</td>\n",
       "      <td>2020-05-29 20:01:25</td>\n",
       "      <td>w7</td>\n",
       "      <td>吹水段到静音房急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618203</td>\n",
       "      <td>2020-05-29 20:01:51</td>\n",
       "      <td>26</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>429187</td>\n",
       "      <td>w7</td>\n",
       "      <td>真空线出口急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618093</td>\n",
       "      <td>2020-05-29 20:00:00</td>\n",
       "      <td>w7</td>\n",
       "      <td>真空线出口急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618199</td>\n",
       "      <td>2020-05-29 20:01:38</td>\n",
       "      <td>98</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>429189</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618097</td>\n",
       "      <td>2020-05-29 20:00:07</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618194</td>\n",
       "      <td>2020-05-29 20:01:00</td>\n",
       "      <td>53</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>429193</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618202</td>\n",
       "      <td>2020-05-29 20:01:48</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618299</td>\n",
       "      <td>2020-05-29 20:03:42</td>\n",
       "      <td>114</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>429196</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3618403</td>\n",
       "      <td>2020-05-29 20:05:15</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3618421</td>\n",
       "      <td>2020-05-29 20:06:00</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pause = pd.read_sql_query(sql_pause, engine)\n",
    "df_pause.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>StartLine</th>\n",
       "      <th>StartVarName</th>\n",
       "      <th>StartAlarm</th>\n",
       "      <th>StartID</th>\n",
       "      <th>StartTime</th>\n",
       "      <th>EndLine</th>\n",
       "      <th>EndVarName</th>\n",
       "      <th>EndAlarm</th>\n",
       "      <th>EndID</th>\n",
       "      <th>EndTime</th>\n",
       "      <th>Interval</th>\n",
       "      <th>isActive</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>StartTime</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:01:25</th>\n",
       "      <td>423316</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554519</td>\n",
       "      <td>2020-05-28 20:01:25</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554522</td>\n",
       "      <td>2020-05-28 20:01:31</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:00:48</th>\n",
       "      <td>423318</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554426</td>\n",
       "      <td>2020-05-28 20:00:48</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554505</td>\n",
       "      <td>2020-05-28 20:00:53</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:03:27</th>\n",
       "      <td>423321</td>\n",
       "      <td>w7</td>\n",
       "      <td>上盖板段急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554644</td>\n",
       "      <td>2020-05-28 20:03:27</td>\n",
       "      <td>w7</td>\n",
       "      <td>上盖板段急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554655</td>\n",
       "      <td>2020-05-28 20:03:50</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:03:16</th>\n",
       "      <td>423322</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554638</td>\n",
       "      <td>2020-05-28 20:03:16</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554646</td>\n",
       "      <td>2020-05-28 20:03:29</td>\n",
       "      <td>13</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:06:19</th>\n",
       "      <td>423326</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554792</td>\n",
       "      <td>2020-05-28 20:06:19</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554799</td>\n",
       "      <td>2020-05-28 20:06:29</td>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>StartLine</th>\n",
       "      <th>StartVarName</th>\n",
       "      <th>StartAlarm</th>\n",
       "      <th>StartID</th>\n",
       "      <th>StartTime</th>\n",
       "      <th>EndLine</th>\n",
       "      <th>EndVarName</th>\n",
       "      <th>EndAlarm</th>\n",
       "      <th>EndID</th>\n",
       "      <th>EndTime</th>\n",
       "      <th>Interval</th>\n",
       "      <th>isActive</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>StartTime</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:01:25</th>\n",
       "      <td>423316</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554519</td>\n",
       "      <td>2020-05-28 20:01:25</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554522</td>\n",
       "      <td>2020-05-28 20:01:31</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:00:48</th>\n",
       "      <td>423318</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554426</td>\n",
       "      <td>2020-05-28 20:00:48</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554505</td>\n",
       "      <td>2020-05-28 20:00:53</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:03:27</th>\n",
       "      <td>423321</td>\n",
       "      <td>w7</td>\n",
       "      <td>上盖板段急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554644</td>\n",
       "      <td>2020-05-28 20:03:27</td>\n",
       "      <td>w7</td>\n",
       "      <td>上盖板段急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554655</td>\n",
       "      <td>2020-05-28 20:03:50</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:03:16</th>\n",
       "      <td>423322</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554638</td>\n",
       "      <td>2020-05-28 20:03:16</td>\n",
       "      <td>w7</td>\n",
       "      <td>线头第二段链板线</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554646</td>\n",
       "      <td>2020-05-28 20:03:29</td>\n",
       "      <td>13</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-05-28 20:06:19</th>\n",
       "      <td>423326</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停按下</td>\n",
       "      <td>3554792</td>\n",
       "      <td>2020-05-28 20:06:19</td>\n",
       "      <td>w7</td>\n",
       "      <td>静音房段到夹抱机急停</td>\n",
       "      <td>急停恢复</td>\n",
       "      <td>3554799</td>\n",
       "      <td>2020-05-28 20:06:29</td>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_pause.set_index(\"StartTime\",inplace=True,drop=False)\n",
    "df_pause.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StartTime  StartVarName\n20         上侧板段急停1             64\n           上盖板段急停             489\n           吹水段到静音房急停          222\n           商检房出口急停             16\n           安参段急停               16\n           真空线出口急停             21\n           线头第一段链板线           113\n           线头第二段链板线            36\n           线尾包装段急停            264\n           静音房段到夹抱机急停         426\n21         上盖板段急停             358\n           吹水段到静音房急停          130\n           安参段急停                9\n           真空线出口急停            105\n           真空线插线急停             10\n           线头第一段链板线           314\n           线头第二段链板线            16\n           线尾包装段急停            207\n           静音房段到夹抱机急停        1039\n22         上盖板段急停             215\n           吹水段到静音房急停          240\n           安参段急停               53\n           真空线插线急停             31\n           线头回板机急停         152170\n           线头第一段链板线           594\n           线头第二段链板线            16\n           线尾包装段急停            338\n           静音房段到夹抱机急停         426\n23         上盖板段急停             341\n           吹水段到静音房急停         4282\n           商检房出口急停         129123\n           安参段急停             3967\n           真空线出口急停              8\n           真空线插线急停           5514\n           线头第一段链板线          4382\n           线头第二段链板线            10\n           线尾包装段急停            116\n           静音房段到夹抱机急停         198\nName: Interval, dtype: int64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped = df_pause.groupby([df_pause.index.hour,\"StartVarName\"])\n",
    "grouped[\"Interval\"].sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "scrolled": true,
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StartVarName\n上盖板段急停        489\n静音房段到夹抱机急停    426\n线尾包装段急停       264\n吹水段到静音房急停     222\n线头第一段链板线      113\nName: Interval, dtype: int64"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped[\"Interval\"].sum()[20].sort_values(ascending=False).head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 筛选包含关键字的数据（以下是节拍）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>VarName</th>\n",
       "      <th>AlarmType</th>\n",
       "      <th>DateTime</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DateTime</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:01:41</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:01:41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:17:51</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:17:51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:41:26</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:41:26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:46:12</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:46:12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:54:22</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:54:22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       VarName AlarmType            DateTime\n",
       "DateTime                                                    \n",
       "2020-05-26 20:01:41  商检房出口直通按钮         开 2020-05-26 20:01:41\n",
       "2020-05-26 20:17:51  商检房出口直通按钮         开 2020-05-26 20:17:51\n",
       "2020-05-26 20:41:26  商检房出口直通按钮         开 2020-05-26 20:41:26\n",
       "2020-05-26 20:46:12  商检房出口直通按钮         开 2020-05-26 20:46:12\n",
       "2020-05-26 20:54:22  商检房出口直通按钮         开 2020-05-26 20:54:22"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_keyword = df.loc[(df[\"VarName\"].str.contains(\"直通\")), [\"VarName\", \"AlarmType\", \"DateTime\"]]\n",
    "df_keyword.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>VarName</th>\n",
       "      <th>AlarmType</th>\n",
       "      <th>DateTime</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DateTime</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:01:41</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:01:41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:17:51</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:17:51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:41:26</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:41:26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:46:12</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:46:12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-26 20:54:22</td>\n",
       "      <td>商检房出口直通按钮</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-26 20:54:22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       VarName AlarmType            DateTime\n",
       "DateTime                                                    \n",
       "2020-05-26 20:01:41  商检房出口直通按钮         开 2020-05-26 20:01:41\n",
       "2020-05-26 20:17:51  商检房出口直通按钮         开 2020-05-26 20:17:51\n",
       "2020-05-26 20:41:26  商检房出口直通按钮         开 2020-05-26 20:41:26\n",
       "2020-05-26 20:46:12  商检房出口直通按钮         开 2020-05-26 20:46:12\n",
       "2020-05-26 20:54:22  商检房出口直通按钮         开 2020-05-26 20:54:22"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for word in replace_words:\n",
    "    df_keyword[\"VarName\"] = df_keyword[\"VarName\"].str.replace(word,\"\")\n",
    "df_keyword.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "mins_20=df_keyword.resample('20T')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2,\n",
       " 0,\n",
       " 4,\n",
       " 4,\n",
       " 0,\n",
       " 0,\n",
       " 1,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 1,\n",
       " 0,\n",
       " 0,\n",
       " 5,\n",
       " 1,\n",
       " 1,\n",
       " 1,\n",
       " 2,\n",
       " 0,\n",
       " 2,\n",
       " 0,\n",
       " 0,\n",
       " 2,\n",
       " 4,\n",
       " 2,\n",
       " 0,\n",
       " 0,\n",
       " 4,\n",
       " 3,\n",
       " 1,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 1,\n",
       " 0,\n",
       " 1,\n",
       " 1,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 3,\n",
       " 3,\n",
       " 4,\n",
       " 2,\n",
       " 4,\n",
       " 3,\n",
       " 2,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 0,\n",
       " 2,\n",
       " 0,\n",
       " 1,\n",
       " 1,\n",
       " 0,\n",
       " 1]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mins_20[\"VarName\"].count().to_list()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'Column not found: -1'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-47-4f7d6c9999bd>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mmins_20\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\pandas\\core\\base.py\u001b[0m in \u001b[0;36m__getitem__\u001b[1;34m(self, key)\u001b[0m\n\u001b[0;32m    266\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    267\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mkey\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 268\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Column not found: {key}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    269\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_gotitem\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mndim\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    270\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'Column not found: -1'"
     ],
     "output_type": "error"
    }
   ],
   "source": [
    "mins_20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>VarName</th>\n",
       "      <th>AlarmType</th>\n",
       "      <th>DateTime</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DateTime</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:00</td>\n",
       "      <td>真空线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:01</td>\n",
       "      <td>卤检节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:05</td>\n",
       "      <td>商检线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:05</td>\n",
       "      <td>封尾节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:07</td>\n",
       "      <td>上压机节拍</td>\n",
       "      <td>关</td>\n",
       "      <td>2020-05-27 08:00:07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:10</td>\n",
       "      <td>真空线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:15</td>\n",
       "      <td>商检线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:21</td>\n",
       "      <td>安参节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:22</td>\n",
       "      <td>上压机节拍</td>\n",
       "      <td>关</td>\n",
       "      <td>2020-05-27 08:00:22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:22</td>\n",
       "      <td>封尾节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:26</td>\n",
       "      <td>商检线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:35</td>\n",
       "      <td>商检线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:35</td>\n",
       "      <td>安参节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:36</td>\n",
       "      <td>卤检节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:36</td>\n",
       "      <td>真空线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:38</td>\n",
       "      <td>上压机节拍</td>\n",
       "      <td>关</td>\n",
       "      <td>2020-05-27 08:00:38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:40</td>\n",
       "      <td>封尾节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:35</td>\n",
       "      <td>商检线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:35</td>\n",
       "      <td>安参节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:36</td>\n",
       "      <td>卤检节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:36</td>\n",
       "      <td>真空线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:38</td>\n",
       "      <td>上压机节拍</td>\n",
       "      <td>关</td>\n",
       "      <td>2020-05-27 08:00:38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:40</td>\n",
       "      <td>封尾节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:47</td>\n",
       "      <td>真空线节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:48</td>\n",
       "      <td>卤检节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:53</td>\n",
       "      <td>上压机节拍</td>\n",
       "      <td>关</td>\n",
       "      <td>2020-05-27 08:00:53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:55</td>\n",
       "      <td>产量节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:00:57</td>\n",
       "      <td>封尾节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:00:57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:01:00</td>\n",
       "      <td>卤检节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:01:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:01:01</td>\n",
       "      <td>安参节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:01:01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:01:20</td>\n",
       "      <td>产量节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:01:20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:01:21</td>\n",
       "      <td>安参节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:01:21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:02:07</td>\n",
       "      <td>产量节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:02:07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:02:09</td>\n",
       "      <td>产量节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:02:09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2020-05-27 08:08:55</td>\n",
       "      <td>产量节拍</td>\n",
       "      <td>开</td>\n",
       "      <td>2020-05-27 08:08:55</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    VarName AlarmType            DateTime\n",
       "DateTime                                                 \n",
       "2020-05-27 08:00:00   真空线节拍         开 2020-05-27 08:00:00\n",
       "2020-05-27 08:00:01    卤检节拍         开 2020-05-27 08:00:01\n",
       "2020-05-27 08:00:05   商检线节拍         开 2020-05-27 08:00:05\n",
       "2020-05-27 08:00:05    封尾节拍         开 2020-05-27 08:00:05\n",
       "2020-05-27 08:00:07   上压机节拍         关 2020-05-27 08:00:07\n",
       "2020-05-27 08:00:10   真空线节拍         开 2020-05-27 08:00:10\n",
       "2020-05-27 08:00:15   商检线节拍         开 2020-05-27 08:00:15\n",
       "2020-05-27 08:00:21    安参节拍         开 2020-05-27 08:00:21\n",
       "2020-05-27 08:00:22   上压机节拍         关 2020-05-27 08:00:22\n",
       "2020-05-27 08:00:22    封尾节拍         开 2020-05-27 08:00:22\n",
       "2020-05-27 08:00:26   商检线节拍         开 2020-05-27 08:00:26\n",
       "2020-05-27 08:00:35   商检线节拍         开 2020-05-27 08:00:35\n",
       "2020-05-27 08:00:35    安参节拍         开 2020-05-27 08:00:35\n",
       "2020-05-27 08:00:36    卤检节拍         开 2020-05-27 08:00:36\n",
       "2020-05-27 08:00:36   真空线节拍         开 2020-05-27 08:00:36\n",
       "2020-05-27 08:00:38   上压机节拍         关 2020-05-27 08:00:38\n",
       "2020-05-27 08:00:40    封尾节拍         开 2020-05-27 08:00:40\n",
       "2020-05-27 08:00:35   商检线节拍         开 2020-05-27 08:00:35\n",
       "2020-05-27 08:00:35    安参节拍         开 2020-05-27 08:00:35\n",
       "2020-05-27 08:00:36    卤检节拍         开 2020-05-27 08:00:36\n",
       "2020-05-27 08:00:36   真空线节拍         开 2020-05-27 08:00:36\n",
       "2020-05-27 08:00:38   上压机节拍         关 2020-05-27 08:00:38\n",
       "2020-05-27 08:00:40    封尾节拍         开 2020-05-27 08:00:40\n",
       "2020-05-27 08:00:47   真空线节拍         开 2020-05-27 08:00:47\n",
       "2020-05-27 08:00:48    卤检节拍         开 2020-05-27 08:00:48\n",
       "2020-05-27 08:00:53   上压机节拍         关 2020-05-27 08:00:53\n",
       "2020-05-27 08:00:55    产量节拍         开 2020-05-27 08:00:55\n",
       "2020-05-27 08:00:57    封尾节拍         开 2020-05-27 08:00:57\n",
       "2020-05-27 08:01:00    卤检节拍         开 2020-05-27 08:01:00\n",
       "2020-05-27 08:01:01    安参节拍         开 2020-05-27 08:01:01\n",
       "2020-05-27 08:01:20    产量节拍         开 2020-05-27 08:01:20\n",
       "2020-05-27 08:01:21    安参节拍         开 2020-05-27 08:01:21\n",
       "2020-05-27 08:02:07    产量节拍         开 2020-05-27 08:02:07\n",
       "2020-05-27 08:02:09    产量节拍         开 2020-05-27 08:02:09\n",
       "2020-05-27 08:08:55    产量节拍         开 2020-05-27 08:08:55"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped_keyword = df_keyword.groupby(\"VarName\")\n",
    "grouped_keyword.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "上压机节拍    16.984615\n",
       "产量节拍     32.538462\n",
       "卤检节拍     16.920635\n",
       "商检线节拍    22.846154\n",
       "安参节拍     22.390244\n",
       "封尾节拍     16.603175\n",
       "真空线节拍    17.333333\n",
       "dtype: float64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_pd = pd.DataFrame({})\n",
    "for group in grouped_keyword.groups:\n",
    "    pd_1 = grouped_keyword.get_group(group)\n",
    "    # DateTime列每个数据减去前一个数据\n",
    "    pd_2 = pd_1[\"DateTime\"].diff().dt.seconds\n",
    "    # 筛选出结果大于0的节拍\n",
    "    npd = pd_2.loc[pd_2 > 0 ]\n",
    "    npd = npd.loc[npd < 180 ]\n",
    "    # 重置索引后存入空DateTime\n",
    "    all_pd[group] = npd.reset_index(drop=True)\n",
    "all_pd.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "上压机节拍    1104.0\n",
       "产量节拍      423.0\n",
       "卤检节拍     1066.0\n",
       "商检线节拍     891.0\n",
       "安参节拍      918.0\n",
       "封尾节拍     1046.0\n",
       "真空线节拍    1092.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "all_pd.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
